<?php

/**
 * Controller is the customized base controller class.
 * All controller classes for this application should extend from this base class.
 * Example:
 * FormDataValidate::instance()->run(array('func'=>'email','value'=>'11111qq.com','title'=>'emai','null'=>'不要为空啊!','msg'=>'请输入正确邮箱!'));
  FormDataValidate::instance()->run(array('func'=>'mob','value'=>'1871108823','title'=>'mob','null'=>'不要为空啊!','msg'=>'请输入正确手机!'));
  FormDataValidate::instance()->run(array('func'=>'tel','value'=>'2856','title'=>'tel','null'=>'不要为空啊!','msg'=>'请输入正确电话!'));
  FormDataValidate::instance()->run(array('func'=>'qq','value'=>'qw333','title'=>'qq','null'=>'不要为空啊!','msg'=>'请输入正确QQ!'));
  FormDataValidate::instance()->run(array('func'=>'string','value'=>'11111','title'=>'name','null'=>'不要为空啊!','max'=>4));
  FormDataValidate::instance()->run(array('func'=>'string','value'=>'11111','title'=>'name1','null'=>'不要为空啊!','min'=>6));
  FormDataValidate::instance()->run(array('func'=>'string','value'=>'','title'=>'name2','null'=>'不要为空啊!','min'=>4,'max'=>6));
  FormDataValidate::instance()->run(array('func'=>'string','value'=>'11111','title'=>'name3','null'=>'不要为空啊!','min'=>6,'max'=>10));
  FormDataValidate::instance()->run(array('func'=>'int','value'=>'1w111','title'=>'int','null'=>'不要为空啊!','min'=>6,'max'=>10));
  FormDataValidate::instance()->run(array('func'=>'int','value'=>'1','title'=>'int1','null'=>'不要为空啊!','min'=>6,'max'=>10));
  FormDataValidate::instance()->run(array('func'=>'int','value'=>'1','title'=>'int2','null'=>'不要为空啊!','min'=>6));
  FormDataValidate::instance()->run(array('func'=>'int','value'=>'11','title'=>'int3','null'=>'不要为空啊!','max'=>10));
  $error = FormDataValidate::instance()->getError();
 *
 */
class FormDataValidate {

    public $error;
    private static $_instance; //定义静态对象
    public $params;
    public $value;

    public static function instance() {
        if (!self::$_instance) {
            self::$_instance = new FormDataValidate();
        }
        return new FormDataValidate();
    }

    //做封装的时候用到
    public function __call($func, $params) {
        $this->params = setDefault($params, 0);
        $this->value = setDefault($this->params, 'value');
        $this->notnull();

        if (is_string($func)) {
            if (method_exists($this, setDefault($this->params, 'func'))) {
                return call_user_func_array(array($this, setDefault($this->params, 'func')), array('set' => 0));
            }
        } else if (is_array($func)) {
            foreach ($func as $val) {
                $status = call_user_func_array(array($this, $val), array('set' => 1));
                if (!status)
                    return;
            }
            $this->setError(setDefault($this->params, 'title'), setDefault($this->params, 'msg'));
        }
    }

    private function string($set) {
        if (isset($this->params['min']) || isset($this->params['max'])) {
            // 去除html标签
            $string = trim(setDefault($this->params, 'value'));
            // 判断内容长度是否在指定的范围。
            $length = mb_strlen($string, 'utf8');
            if (isset($this->params['min']) && isset($this->params['max'])) {
                if ($length < setDefault($this->params, 'min') || $length > setDefault($this->params, 'max')) {
                    if ($set)
                        return true;
                    $this->setError(setDefault($this->params, 'title'), "该信息长度在" . setDefault($this->params, 'min') . "到" . setDefault($this->params, 'max') . "个汉字之间，请修改使其更加合理");
                }
                return;
            }
            if (isset($this->params['min'])) {
                if ($length < setDefault($this->params, 'min')) {
                    if ($set)
                        return true;
                    $this->setError(setDefault($this->params, 'title'), "该信息长度小于" . setDefault($this->params, 'min') . "个汉字之间，请修改使其更加合理");
                }
                return;
            }
            if (isset($this->params['max'])) {
                if ($length > setDefault($this->params, 'max')) {
                    if ($set)
                        return true;
                    $this->setError(setDefault($this->params, 'title'), "该信息长度大于" . setDefault($this->params, 'max') . "个汉字之间，请修改使其更加合理");
                }
                return;
            }
        }
    }

    private function email($set) {
        if (preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/", $this->value)) {
            return;
        } else {
            if ($set)
                return true;
            $this->setError(setDefault($this->params, 'title'), setDefault($this->params, 'msg', '请输入正确电子邮箱!'));
        }
    }

    private function mob($set) {
        if (preg_match("/^1[3456789]{1}[0-9]{1}[0-9]{8}$/", $this->value)) {
            return;
        } else {
            if ($set)
                return true;
            $this->setError(setDefault($this->params, 'title'), setDefault($this->params, 'msg', '请输入正确手机号码!'));
        }
    }

    private function tel($set) {
        if (preg_match("/\d{3}-\d{8}|\d{4}-\d{7}/", $this->value)) {
            return;
        } else {
            if ($set)
                return true;
            $this->setError(setDefault($this->params, 'title'), setDefault($this->params, 'msg', '请输入正确电话号码!'));
        }
    }

    private function qq($set) {
        if (preg_match("/[1-9][0-9]{4,}/", $this->value)) {
            return;
        } else {
            if ($set)
                return true;
            $this->setError(setDefault($this->params, 'title'), setDefault($this->params, 'msg', '请输入正确QQ号!'));
        }
    }

    private function int($set) {
        if (is_numeric($this->value) && (int) $this->value == $this->value) {
            
        } else {
            if ($set)
                return true;
            $this->setError(setDefault($this->params, 'title'), setDefault($this->params, 'msg', '请输入数字!'));
            return;
        }

        if (isset($this->params['min']) && isset($this->params['max'])) {
            if ($this->value < $this->params['min'] || $this->value > $this->params['max']) {
                if ($set)
                    return true;
                $this->setError(setDefault($this->params, 'title'), "该信息长度在" . setDefault($this->params, 'min') . "到" . setDefault($this->params, 'max') . "个汉字之间，请修改使其更加合理");
            }
            return;
        }

        if (isset($this->params['max'])) {
            if ($this->value > $this->params['max']) {
                if ($set)
                    return true;
                $this->setError(setDefault($this->params, 'title'), "该数字大于" . setDefault($this->params, 'max') . "，请修改使其更加合理");
            }
            return;
        }

        if (isset($this->params['min'])) {
            if ($this->value < $this->params['min']) {
                if ($set)
                    return true;
                $this->setError(setDefault($this->params, 'title'), "该数字小于" . setDefault($this->params, 'min') . "，请修改使其更加合理");
            }
            return;
        }
    }

    private function num($set) {
        if (is_numeric($this->value)) {

        } else {
            if ($set)
                return true;
            $this->setError(setDefault($this->params, 'title'), setDefault($this->params, 'msg', '请输入数字!'));
            return;
        }

        if (isset($this->params['min']) && isset($this->params['max'])) {
            if ($this->value < $this->params['min'] || $this->value > $this->params['max']) {
                if ($set)
                    return true;
                $this->setError(setDefault($this->params, 'title'), "该信息长度在" . setDefault($this->params, 'min') . "到" . setDefault($this->params, 'max') . "个汉字之间，请修改使其更加合理");
            }
            return;
        }

        if (isset($this->params['max'])) {
            if ($this->value > $this->params['max']) {
                if ($set)
                    return true;
                $this->setError(setDefault($this->params, 'title'), "该数字大于" . setDefault($this->params, 'max') . "，请修改使其更加合理");
            }
            return;
        }

        if (isset($this->params['min'])) {
            if ($this->value < $this->params['min']) {
                if ($set)
                    return true;
                $this->setError(setDefault($this->params, 'title'), "该数字小于" . setDefault($this->params, 'min') . "，请修改使其更加合理");
            }
            return;
        }
    }

    private function notnull() {
        if (isset($this->params['null'])) {
            if (!is_string($this->value) && empty($this->value)) {
                $this->setError(setDefault($this->params, 'title'), setDefault($this->params, 'null', '请输入对应信息!'));
            }
        }
    }

    private function setError($key, $msg) {
        self::$_instance->error[$key] = $msg;
    }

    public function getError() {
        return self::$_instance->error;
    }

}